package com.yangle.frame.admin.mapper;


import com.yangle.frame.MyMapper;
import com.yangle.frame.admin.entity.FrameTable;
import org.apache.ibatis.annotations.*;
import org.springframework.beans.factory.annotation.Value;

import java.util.List;

@Mapper
public interface FrameTableMapper extends MyMapper<FrameTable> {


    /**
     * 批量删除
     */
    @Delete("<script> delete from frame_table where id in "
            + "<foreach item='item' index='index' collection='ids' open='(' separator=',' close=')'>"
            + "#{item}"
            + "</foreach> </script>")
    void deletes(@Param("ids") List<String> ids);


    /**
     * 查询表是否存在
     */
    @Select("select count(1) from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA=#{databasename} and TABLE_NAME=#{tableName}")
    @ResultType(value = int.class)
    int queryTableIsExists(@Param("databasename") String databasename, @Param("tableName") String tableName);

    /**
     * 查询字段是否存在
     *
     * @param databasename
     * @param tableName
     * @param fieldName
     * @return
     */
    @ResultType(value = int.class)
    @Select("select count(1) from INFORMATION_SCHEMA.COLUMNS where TABLE_SCHEMA=#{databasename} and TABLE_NAME=#{tableName} AND COLUMN_NAME = #{fieldName}")
    int queryFieldIsExists(@Param("databasename") String databasename, @Param("tableName") String tableName, @Param("fieldName") String fieldName);

    /**
     * 修改字段
     *
     * @param tableName
     * @param fieldName
     * @param fieldType
     */
    @Update("ALTER TABLE ${tableName} CHANGE ${preFieldName} ${fieldName} ${fieldType}  COMMENT '${description}'")
    void alterFieldAttr(@Param("preFieldName") String preFieldName, @Param("tableName") String tableName, @Param("fieldName") String fieldName, @Param("fieldType") String fieldType, @Param("description") String description);

    /**
     * 添加字段
     */
    @Update("alter table ${tableName} add ${fieldName} ${fieldType} COMMENT '${description}'")
    void addField(@Param("tableName") String tableName, @Param("fieldName") String fieldName, @Param("fieldType") String fieldType,@Param("description") String description);

    /**
     * 删除字段
     */
    @Update("alter table ${tableName} DROP COLUMN ${fieldName}")
    void dropField(@Param("tableName") String tableName, @Param("fieldName") String fieldName);
}
